import { App, getCurrentInstance, Plugin } from "vue";
import mitt, { Emitter } from "mitt";
import { TabInfo } from "@/bean/vo/TabInfo";

type Events = {
  cacheKeysChange: string[];
  tabsChange: TabInfo[];
}

export function useBus() {
  const instance = getCurrentInstance();
  return instance.appContext.config.globalProperties.$bus;
}

/** 事件总线插件 */
export default <Plugin<any[]>>{
  install: function (app: App) {
    app.config.globalProperties.$bus = mitt<Events>();
  }
}

declare module "@vue/runtime-core" {
  interface ComponentCustomProperties {
    $bus: Emitter<Events>;
  }
}
